﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyQuartz.Domain.Services.Scheduling;
using EasyQuartz.Web.ViewModels;
using System.Runtime.Remoting;
using EasyQuartz.Domain.Scheduler;
using EasyQuartz.Web.Models;

namespace EasyQuartz.Web.Controllers
{
	[HandleError]
	public class HomeController : Controller
	{
		//ISchedulingManager scheduler = SchedulerInstance.Value;
		ISchedulingManager scheduler = new SchedulingManager();

		public ActionResult Index()
		{
			var viewModel = new SchedulerStatusViewModel();

			try
			{
				viewModel.SchedulerStatus = scheduler.Status;
			}
			catch (Exception)
			{
				viewModel.SchedulerStatus = EasySchedulerStatus.Down;
			}

			return View(viewModel);
		}

		public ActionResult Start()
		{
			try
			{
				scheduler.Start();
				return View(EasySchedulerStatus.Active.ToString());
			}
			catch (Exception e)
			{
				return Json(e.Message);
			}
		}

		public ActionResult Stop()
		{
			try
			{
				scheduler.Stop();
				return View(EasySchedulerStatus.Inactive.ToString());
			}
			catch (Exception e)
			{
				return Json(e.Message);
			}
		}
	}
}
